<template>
<div class="lock-container">
    <basic-container>
        <div style="margin-left:10%;margin-bottom: 20px;font-size: 14px;" v-if="$route.query.finished == 'true'">
            <el-button icon="el-icon-edit-outline" type="success" size="mini" @click="handleComplete">审批</el-button>
            <el-button icon="el-icon-refresh-left" type="warning" size="mini" @click="handleReturn">退回</el-button>
            <el-button icon="el-icon-circle-close" type="danger" size="mini" @click="handleReject">驳回</el-button>
        </div>
        <component :is="$route.query.comName"></component>
    </basic-container>

    <!--审批正常流程-->
    <el-dialog :title="completeTitle" :visible.sync="completeOpen" :width="checkSendUser? '60%':'40%'" append-to-body>
        <el-form ref="taskForm" :model="taskForm" label-width="80px">
            <el-form-item v-if="checkSendUser" prop="targetKey">
                <el-row :gutter="20">
                    <!--部门数据-->
                    <el-col :span="6" :xs="24">
                        <h6>部门列表</h6>
                        <div class="head-container">
                            <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search" style="margin-bottom: 20px" />
                        </div>
                        <div class="head-container">
                            <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false" :filter-node-method="filterNode" ref="tree" default-expand-all @node-click="handleNodeClick" />
                        </div>
                    </el-col>
                    <el-col :span="10" :xs="24">
                        <h6>待选人员</h6>
                        <el-table ref="singleTable" :data="userList" border style="width: 100%" @selection-change="handleSelectionChange">
                            <el-table-column type="selection" width="50" align="center" />
                            <el-table-column label="用户名" align="center" prop="realName" />
                            <el-table-column label="部门" align="center" prop="deptName" />
                        </el-table>
                    </el-col>
                    <el-col :span="8" :xs="24">
                        <h6>已选人员</h6>
                        <el-tag v-for="(user,index) in userData" :key="index" closable @close="handleClose(user)">
                            {{user.realName}} {{user.deptName}}
                        </el-tag>
                    </el-col>
                </el-row>
            </el-form-item>
            <el-form-item label="处理意见" prop="comment" :rules="[{ required: true, message: '请输入处理意见', trigger: 'blur' }]">
                <el-input type="textarea" v-model="taskForm.comment" placeholder="请输入处理意见" />
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="completeOpen = false">取 消</el-button>
            <el-button type="primary" @click="taskComplete">确 定</el-button>
        </span>
    </el-dialog>

    <!--退回流程-->
    <el-dialog :title="returnTitle" :visible.sync="returnOpen" width="40%" append-to-body>
        <el-form ref="taskForm" :model="taskForm" label-width="80px">
            <el-form-item label="退回节点" prop="targetKey">
                <el-radio-group v-model="taskForm.targetKey">
                    <el-radio-button v-for="item in returnTaskList" :key="item.id" :label="item.id">{{item.name}}</el-radio-button>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="退回意见" prop="comment" :rules="[{ required: true, message: '请输入意见', trigger: 'blur' }]">
                <el-input style="width: 50%" type="textarea" v-model="taskForm.comment" placeholder="请输入意见" />
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="returnOpen = false">取 消</el-button>
            <el-button type="primary" @click="taskReturn">确 定</el-button>
        </span>
    </el-dialog>

    <!--驳回流程-->
    <el-dialog :title="rejectTitle" :visible.sync="rejectOpen" width="40%" append-to-body>
        <el-form ref="taskForm" :model="taskForm" label-width="80px">
            <el-form-item label="驳回意见" prop="comment" :rules="[{ required: true, message: '请输入意见', trigger: 'blur' }]">
                <el-input style="width: 50%" type="textarea" v-model="taskForm.comment" placeholder="请输入意见" />
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="rejectOpen = false">取 消</el-button>
            <el-button type="primary" @click="taskReject">确 定</el-button>
        </span>
    </el-dialog>
</div>
</template>

<script>
import myform from "./util/form"
import mytable from "./util/table.vue";

import { detailProcess } from '@/api/workflow/process'
import { complete, delegate, transfer, rejectTask, returnList, returnTask } from '@/api/workflow/task'
import {
    getDeptTree
} from "@/api/system/dept";
import {
    getList
} from "@/api/system/user";

export default {
    name: "weltest",
    components: {
        myform,
        mytable
    },
    data() {
        return {
            completeTitle: null,
            completeOpen: false,
            returnTitle: null,
            returnOpen: false,
            rejectOpen: false,
            rejectTitle: null,
            taskForm: {
                returnTaskShow: false, // 是否展示回退表单
                delegateTaskShow: false, // 是否展示回退表单
                defaultTaskShow: true, // 默认处理
                sendUserShow: false, // 审批用户
                multiple: false,
                comment: "", // 意见内容
                procInsId: "", // 流程实例编号
                instanceId: "", // 流程实例编号
                deployId: "", // 流程定义编号
                taskId: "", // 流程任务编号
                procDefId: "", // 流程编号
                vars: "",
                targetKey: ""
            },
            returnTaskList: [], // 回退列表数据
            // 部门名称
            deptName: undefined,
            // 部门树选项
            deptOptions: undefined,
            // 用户表格数据
            userList: null,
            userData: [],
            defaultProps: {
                children: "children",
                label: "title"
            },
            // 查询参数
            queryParams: {
                deptId: undefined
            },
            userDataList: [], // 流程候选人
            checkSendUser: false // 是否展示选择人员模块
        };
    },
    computed: {},
    created() {
        this.taskForm.deployId = this.$route.query && this.$route.query.deployId;
        this.taskForm.taskId = this.$route.query && this.$route.query.taskId;
        this.taskForm.procInsId = this.$route.query && this.$route.query.procInsId;
        this.taskForm.executionId = this.$route.query && this.$route.query.executionId;
        this.taskForm.instanceId = this.$route.query && this.$route.query.procInsId;
        // 初始化表单
        this.taskForm.procDefId = this.$route.query && this.$route.query.procDefId;
        // 回显流程记录
        // this.getFlowViewer(this.taskForm.procInsId, this.taskForm.executionId);
        // this.getModelDetail(this.taskForm.deployId);
        // 流程任务重获取变量表单
        if (this.taskForm.taskId) {
            this.getProcessDetails(this.taskForm.taskId)
            this.taskForm.deployId = null
        }
        // this.getFlowRecordList(this.taskForm.procInsId, this.taskForm.deployId);
        this.finished = this.$route.query && this.$route.query.finished
    },
    methods: {
        /** 查询部门下拉树结构 */
        getTreeselect() {
            getDeptTree().then(response => {
                this.deptOptions = response.data.data;
            });
        },
        /** 查询用户列表 */
        getList() {
            getList(1, 1000, this.queryParams).then(response => {
                this.userList = response.data.data.records;
                this.total = response.data.data.total;
            });
        },
        // 筛选节点
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        // 节点单击事件
        handleNodeClick(data) {
            this.queryParams.deptId = data.id;
            this.getList();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            if (selection) {
                this.userData = selection
                const selectVal = selection.map(item => item.id);
                if (selectVal instanceof Array) {
                    this.taskForm.values = {
                        "approval": selectVal.join(',')
                    }
                } else {
                    this.taskForm.values = {
                        "approval": selectVal
                    }
                }
            }
        },
        // 关闭标签
        handleClose(tag) {
            this.userData.splice(this.userData.indexOf(tag), 1);
            this.$refs.singleTable.toggleRowSelection(tag, false)
        },
        getProcessDetails(procInsId, deployId, taskId) {
            const params = {procInsId: procInsId, deployId: deployId, taskId: taskId}
            detailProcess(params).then(res => {
                const data = res.data.data;
                this.xmlData = data.bpmnXml;
                this.processFormList = data.processFormList;
                this.taskFormOpen = data.existTaskForm;
                if (this.taskFormOpen) {
                this.taskFormData = data.taskFormData;
                }
                this.historyProcNodeList = data.historyProcNodeList;
                this.finishedInfo = data.flowViewer;
                this.formOpen = true
            })
        },
        /** 审批任务选择 */
        handleComplete() {
            this.completeOpen = true;
            this.completeTitle = "审批流程";
            this.getTreeselect();
        },
        /** 审批任务 */
        taskComplete() {
            if (!this.taskForm.values && this.checkSendUser) {
                this.msgError("请选择流程接收人员");
                return;
            }
            if (!this.taskForm.comment) {
                this.msgError("请输入审批意见");
                return;
            }
            complete(this.taskForm).then(response => {
                this.msgSuccess(response.data.msg);
                this.goBack();
            });
        },
        /** 返回页面 */
        goBack() {
            // 关闭当前标签页并返回上个页面
            //this.$store.dispatch("tagsView/delView", this.$route);
            this.$router.$avueRouter.closeTag()
            this.$router.go(-1)
        },
        /** 可退回任务列表 */
        handleReturn() {
            this.returnOpen = true;
            this.returnTitle = "退回流程";
            returnList(this.taskForm).then(res => {
                this.returnTaskList = res.data.data;
                this.taskForm.values = null;
            })
        },
        /** 提交退回任务 */
        taskReturn() {
            this.$refs["taskForm"].validate(valid => {
                if (valid) {
                    returnTask(this.taskForm).then(res => {
                        this.msgSuccess(res.data.msg);
                        this.goBack()
                    });
                }
            });
        },
        /** 取消回退任务按钮 */
        cancelTask() {
            this.taskForm.returnTaskShow = false;
            this.taskForm.defaultTaskShow = true;
            this.taskForm.sendUserShow = true;
            this.returnTaskList = [];
        },
        /** 驳回任务 */
        handleReject() {
            this.rejectOpen = true;
            this.rejectTitle = "驳回流程";
        },
        /** 驳回任务 */
        taskReject() {
            this.$refs["taskForm"].validate(valid => {
                if (valid) {
                    rejectTask(this.taskForm).then(res => {
                        this.msgSuccess(res.data.msg);
                        this.goBack();
                    });
                }
            });
        },
    }
};
</script>

<style lang="scss" scoped>
.lock-container {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;

    .title {
        margin-bottom: 8px;
        color: #333;
    }
}

.lock-container::before {
    z-index: -999;
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-image: url("/img/bg/login.png");
    background-size: cover;
}
</style>
